package com.codingx.validator.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestResponse<T> {
    /**
     * 约定小于0为失败
     */
    private int code=0;
    private String message="成功";
    private T data;

    /**
     * 无参构造函数
     */
    private RestResponse() {
        super();
    }

    /**
     * 有参构造
     * @param data
     */
    private RestResponse(T data) {
        super();
        this.data = data;
    }
    /**
     * 成功响应.
     * @param data
     * @return
     */
    public static <T> RestResponse<T> succ(T data) {
        return new RestResponse<>(data);
    }
    public static <T> RestResponse<T> succ(T data,String msg) {
        RestResponse<T> ret = new RestResponse<>(data);
        ret.setMessage(msg);
        return ret;
    }
    /**
     * 成功响应.
     * @param data
     * @return
     */
    public static <T> RestResponse<T> fail(T data) {
        return new RestResponse<>(data);
    }
    /**
     * 失败响应
     *
     * @param message
     * @return
     */
    public static <T> RestResponse<T> fail(String message) {
        return fail(-1, message);
    }
    /**
     * 失败响应
     * @param code
     * @param message
     * @return
     */
    public static <T> RestResponse<T> fail(int code, String message) {
        RestResponse<T> ret = new RestResponse<T>();
        ret.setCode(code);
        ret.setMessage(message);
        return ret;
    }
}
